package com.xizi.forum.common;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;

@Data
public class AppResult<T> {    //统一结果返回
    long code;
    String message;

    @JsonInclude(JsonInclude.Include.ALWAYS)
    T data;

    public AppResult(ResultCode resultCode,T data){
        this.code = resultCode.code;
        this.message = resultCode.message;
        this.data = data;
    }

    public AppResult(ResultCode resultCode){
        this.code = resultCode.code;
        this.message = resultCode.message;
        this.data = null;
    }

    public AppResult(long code,String message,T data){
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static <T>AppResult<T> success(){
        return new AppResult<>(ResultCode.SUCCESS,null);
    }

    public static <T>AppResult<T> success(T data){
        return new AppResult<>(ResultCode.SUCCESS,data);
    }


    public static <T>AppResult<T> fail(ResultCode resultCode){
        return new AppResult<>(resultCode);
    }

    public static <T>AppResult<T> fail(T data){
        return new AppResult<>(ResultCode.FAILED,data);
    }

    public static AppResult fail(String message){
        return new AppResult(303,message,null);
    }

}
